﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using iMusik.Resources;
using System.Windows.Media;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using iMusik.Mp3ZingOnline;
using ImageTools.IO.Gif;
using ImageTools;

namespace iMusik
{
    public enum TabSong
    {
        Songs, Videos, Albums,
    };

    public enum Tab
    {
        Home, Top, Top100, Search, HotVideo, NewVideo,
    };

    public enum TopType
    {
        VN, US, KR,
    };

    public enum Top100Type
    {
        NhacThieuNhi, NhacCachMang, NhacTrinh, NhacTre, NhacRapViet, NhacRockViet, NhacTruTinh,
        Pop, Rock, Rap, Country,
    };

    public class Mp3ZingPageControl
    {
        private bool _ShowGMainContent;
        public bool ShowGMainContent
        {
            get { return _ShowGMainContent; }
            set { _ShowGMainContent = value; }
        }

        private bool _ShowGExecuteContentTop;
        public bool ShowGExecuteContentTop
        {
            get { return _ShowGExecuteContentTop; }
            set { _ShowGExecuteContentTop = value; }
        }

        private bool _ShowGExecuteContentTop100;
        public bool ShowGExecuteContentTop100
        {
            get { return _ShowGExecuteContentTop100; }
            set { _ShowGExecuteContentTop100 = value; }
        }

        private bool _ShowGExecuteContentSearch;
        public bool ShowGExecuteContentSearch
        {
            get { return _ShowGExecuteContentSearch; }
            set { _ShowGExecuteContentSearch = value; }
        }

        private bool _ShowGResultContent;
        public bool ShowGResultContent
        {
            get { return _ShowGResultContent; }
            set { _ShowGResultContent = value; }
        }

        private Tab _CurrentTab;
        public Tab CurrentTab
        {
            get { return _CurrentTab; }
            set { _CurrentTab = value; }
        }

        private TabSong _CurrentTabSong;
        public TabSong CurrentTabSong
        {
            get { return _CurrentTabSong; }
            set { _CurrentTabSong = value; }
        }

        private TopType _CurrentTopType;
        public TopType CurrentTopType
        {
            get { return _CurrentTopType; }
            set { _CurrentTopType = value; }
        }

        private Top100Type _CurrentTop100Type;
        public Top100Type CurrentTop100Type
        {
            get { return _CurrentTop100Type; }
            set { _CurrentTop100Type = value; }
        }

        private Mp3Zing _Mp3Zing;
        public Mp3Zing Mp3Zing
        {
            get { return _Mp3Zing; }
            set { _Mp3Zing = value; }
        }

        public Mp3ZingPageControl()
        {
            _ShowGMainContent = false;
            _ShowGExecuteContentTop = false;
            _ShowGExecuteContentTop100 = false;
            _ShowGExecuteContentSearch = false;
            _ShowGResultContent = false;
            _Mp3Zing = null;
        }

        public void SetValue(bool main, bool top, bool top100,
            bool search, bool result)
        {
            _ShowGMainContent = main;
            _ShowGExecuteContentTop = top;
            _ShowGExecuteContentTop100 = top100;
            _ShowGExecuteContentSearch = search;
            _ShowGResultContent = result;
        }
    };

    public partial class Mp3ZingPage : PhoneApplicationPage
    {
        private Mp3Zing _Mp3Zing;
        private List<SongControl> _ListItems;
        private List<Mp3Song> _ListSong;
        private List<Mp3Video> _ListVideo;
        private List<Mp3Album> _ListAlbum;

        private bool _IsSetHome;
        private bool _IsSetTop;
        private bool _IsSetTop100;
        private bool _IsSetHotVideo;
        private Tab _CurrentTab;
        private TabSong _CurrentTabSong;
        private TopType _CurrentTopType;
        private Top100Type _CurrentTop100Type;

        private Mp3ZingPageControl _PageContent;
        private Point _MouseDownPoint;
        private Point _MouseUpPoint;

        public Mp3ZingPage()
        {
            InitializeComponent();
            _ListItems = new List<SongControl>();
            _ListSong = new List<Mp3Song>();
            _ListVideo = new List<Mp3Video>();
            _ListAlbum = new List<Mp3Album>();

            _CurrentTab = Tab.Home;
            _Mp3Zing = new Mp3Zing();

            SetShowHiddenGrid(true, false, false, false, false);

            _MouseDownPoint = new Point();
            _MouseUpPoint = new Point();
            _PageContent = new Mp3ZingPageControl();
            

        }


        #region Cac ham xu li

        private async Task SetSongList()
        {
            switch (_CurrentTab)
            {
                case Tab.Home:
                    if (!_IsSetHome)
                        await SetHome();
                    SetListSongChild(_Mp3Zing.Home.TopListSong);
                    break;
                case Tab.Top:

                    if (!_IsSetTop)
                        await SetTop();
                    if (_CurrentTopType == TopType.VN)
                        SetListSongChild(_Mp3Zing.Top.TopVN.TopListSong);
                    else if (_CurrentTopType == TopType.US)
                        SetListSongChild(_Mp3Zing.Top.TopUS.TopListSong);
                    else
                        SetListSongChild(_Mp3Zing.Top.TopKR.TopListSong);

                    break;
                case Tab.Top100:
                    if (!_IsSetTop100)
                        await SetTop100();
                    switch (_CurrentTop100Type)
                    {
                        case Top100Type.NhacThieuNhi:
                            await _Mp3Zing.Top100.SetTop100NhacThieuNhi();
                            SetListSongChild(_Mp3Zing.Top100.Top100NhacThieuNhi.ListSongs);
                            break;
                        case Top100Type.NhacTrinh:
                            await _Mp3Zing.Top100.SetTop100NhacTrinh();
                            SetListSongChild(_Mp3Zing.Top100.Top100NhacTrinh.ListSongs);
                            break;
                        case Top100Type.NhacTruTinh:
                            await _Mp3Zing.Top100.SetTop100NhacTruTinh();
                            SetListSongChild(_Mp3Zing.Top100.Top100NhacTruTinh.ListSongs);
                            break;
                        case Top100Type.NhacCachMang:
                            await _Mp3Zing.Top100.SetTop100NhacCachMang();
                            SetListSongChild(_Mp3Zing.Top100.Top100NhacCachMang.ListSongs);
                            break;
                        case Top100Type.NhacRapViet:
                            await _Mp3Zing.Top100.SetTop100NhacRapViet();
                            SetListSongChild(_Mp3Zing.Top100.Top100NhacRapViet.ListSongs);
                            break;
                        case Top100Type.NhacRockViet:
                            await _Mp3Zing.Top100.SetTop100NhacRockViet();
                            SetListSongChild(_Mp3Zing.Top100.Top100NhacRockViet.ListSongs);
                            break;
                        case Top100Type.NhacTre:
                            await _Mp3Zing.Top100.SetTop100NhacTre();
                            SetListSongChild(_Mp3Zing.Top100.Top100NhacTre.ListSongs);
                            break;
                        case Top100Type.Pop:
                            await _Mp3Zing.Top100.SetTop100Pop();
                            SetListSongChild(_Mp3Zing.Top100.Top100Pop.ListSongs);
                            break;
                        case Top100Type.Rock:
                            await _Mp3Zing.Top100.SetTop100Rock();
                            SetListSongChild(_Mp3Zing.Top100.Top100Rock.ListSongs);
                            break;
                        case Top100Type.Rap:
                            await _Mp3Zing.Top100.SetTop100Rap();
                            SetListSongChild(_Mp3Zing.Top100.Top100Rap.ListSongs);
                            break;
                        case Top100Type.Country:
                            await _Mp3Zing.Top100.SetTop100Country();
                            SetListSongChild(_Mp3Zing.Top100.Top100Country.ListSongs);
                            break;
                    }
                    break;
                case Tab.Search:
                    SetListSongChild(_Mp3Zing.Search.ResultListSong);
                    break;
            }
        }

        private void SetListSongChild(List<Mp3Song> ListSong)
        {
            _ListItems = new List<SongControl>();
            _ListSong = new List<Mp3Song>();
            BitmapImage bm = new BitmapImage(new Uri(@"http://static.mp3.zdn.vn/skins/mp3_mobile/images/logo-android.png"));
            foreach (Mp3Song song in ListSong)
            {
                SongControl sctrl = new SongControl();
                sctrl.Width = lMainList.Width;
                sctrl.songImage.Source = bm;
                sctrl.SongName = song.SongName;
                sctrl.SingerName = song.SingerName;
                _ListItems.Add(sctrl);
                _ListSong.Add(song);
            }
            lMainList.ItemsSource = _ListItems;
        }

        private async Task SetVideoList()
        {
            switch (_CurrentTab)
            {
                case Tab.Home:
                    if (!_IsSetHome)
                        await SetHome();
                    SetListVideoChild(_Mp3Zing.Home.TopListVideo);
                    break;
                case Tab.HotVideo:
                    if (!_IsSetHotVideo)
                        await SetHotVideo();
                    SetListVideoChild(_Mp3Zing.HotVideo.ListHotVideo);
                    break;
                case Tab.NewVideo:
                    if (!_IsSetHotVideo)
                        await SetHotVideo();
                    SetListVideoChild(_Mp3Zing.HotVideo.ListNewVideo);
                    break;
                case Tab.Search:
                    SetListVideoChild(_Mp3Zing.Search.ResultListVideo);
                    break;
                case Tab.Top:
                    if (!_IsSetTop)
                        await SetTop();
                    if (_CurrentTopType == TopType.VN)
                        SetListVideoChild(_Mp3Zing.Top.TopVN.TopListVideo);
                    else if (_CurrentTopType == TopType.US)
                        SetListVideoChild(_Mp3Zing.Top.TopUS.TopListVideo);
                    else
                        SetListVideoChild(_Mp3Zing.Top.TopKR.TopListVideo);
                    break;

            }
        }

        private void SetListVideoChild(List<Mp3Video> ListVideo)
        {
            _ListItems = new List<SongControl>();
            _ListVideo = new List<Mp3Video>();        
            foreach (Mp3Video video in ListVideo)
            {
                SongControl sctrl = new SongControl();
                sctrl.Width = lMainList.Width;
                sctrl.SrcImg = video.ImgLink;
                sctrl.SongName = video.VideoName;
                sctrl.SingerName = video.SingerName;
                _ListItems.Add(sctrl);
                _ListVideo.Add(video);
            }
            lMainList.ItemsSource = _ListItems;
        }

        private async Task SetAlbumList()
        {
                switch (_CurrentTab)
                {
                    case Tab.Home:
                        if (!_IsSetHome)
                            await SetHome();
                        SetListAlbumChild(_Mp3Zing.Home.TopListAlbum);
                        break;
                    case Tab.Top:
                        if (!_IsSetTop)
                            await SetTop();
                        if (_CurrentTopType == TopType.VN)
                            SetListAlbumChild(_Mp3Zing.Top.TopVN.TopListAlbum);
                        else if (_CurrentTopType == TopType.US)
                            SetListAlbumChild(_Mp3Zing.Top.TopUS.TopListAlbum);
                        else
                            SetListAlbumChild(_Mp3Zing.Top.TopKR.TopListAlbum);
                        break;
                }
            
        }

        private void SetListAlbumChild(List<Mp3Album> ListAlbum)
        {
            _ListItems = new List<SongControl>();
            _ListAlbum = new List<Mp3Album>();
            foreach (Mp3Album album in ListAlbum)
            { 
                SongControl sctrl = new SongControl();
                sctrl.Width = lMainList.Width;
                sctrl.SrcImg = album.ImgLink;
                sctrl.SongName = album.AlbumName;
                sctrl.SingerName = album.SingerName;
                _ListItems.Add(sctrl);
                _ListAlbum.Add(album);
            }
            lMainList.ItemsSource = _ListItems;
        }

        private async Task SetHome()
        {
            try
            {
                if (_IsSetHome)
                    return;
                await _Mp3Zing.SetHome();
                _IsSetHome = true;

            }
            catch(Exception ex)
            {
                throw (ex);
            }

            
            
  
        }

        private async Task SetTop()
        {
            try
            {
                if (_IsSetTop)
                    return;
                await _Mp3Zing.SetTop();

                _IsSetTop = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }


        }

        private async Task SetTop100()
        {
            try
            {
                if (_IsSetTop100)
                    return;
                await _Mp3Zing.SetTop100();
                _IsSetTop100 = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private async Task SetHotVideo()
        {
            try
            {
                if (_IsSetHotVideo)
                    return;
                await _Mp3Zing.SetHotVideo();
                _IsSetHotVideo = true;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        #endregion
        
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            imgLoading.Visibility = System.Windows.Visibility.Collapsed;
            imgLoading.Opacity = 0;
            imgLoading.Stop();

            ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
            imgLoading.Source = new ExtendedImage() { UriSource = new Uri("/Assets/Icons/loading_video.gif", UriKind.Relative) };

            _CurrentTabSong = TabSong.Songs;
            SetShowHiddenGrid(_PageContent.ShowGMainContent, _PageContent.ShowGExecuteContentTop,
                        _PageContent.ShowGExecuteContentTop100, _PageContent.ShowGExecuteContentSearch, _PageContent.ShowGResultContent);
            if (_PageContent.Mp3Zing != null)
            {
                _Mp3Zing = _PageContent.Mp3Zing;
                _CurrentTab = _PageContent.CurrentTab;
                _CurrentTabSong = _PageContent.CurrentTabSong;
                _CurrentTopType = _PageContent.CurrentTopType;
                _CurrentTop100Type = _PageContent.CurrentTop100Type;
                SetMainList();
            }

            if (_CurrentTabSong == TabSong.Videos)
                tblCurrentSongTabs.Text = "Video";

            else if (_CurrentTabSong == TabSong.Songs)
                tblCurrentSongTabs.Text = "Bài Hát";
            else
                tblCurrentSongTabs.Text = "Album";

        }

        #region Grid Song Tab Content Tap

        private async void SetMainList()
        {
            imgLoading.Visibility = System.Windows.Visibility.Visible;
            imgLoading.Opacity = 30;
            imgLoading.Start();
            gErrorNet.Visibility = System.Windows.Visibility.Collapsed;
            gContentResult.Visibility = System.Windows.Visibility.Visible;
            try
            {
                lMainList.ItemsSource = null;

                if (_CurrentTabSong == TabSong.Videos)
                    tblCurrentSongTabs.Text = "Video";

                else if (_CurrentTabSong == TabSong.Songs)
                    tblCurrentSongTabs.Text = "Bài Hát";
                else
                    tblCurrentSongTabs.Text = "Album";
                if (_CurrentTabSong == TabSong.Songs)
                    await SetSongList();
                else if (_CurrentTabSong == TabSong.Videos)
                    await SetVideoList();
                else
                    await SetAlbumList();
                imgLoading.Opacity = 0;
                imgLoading.Stop();
                imgLoading.Visibility = System.Windows.Visibility.Collapsed;
                gErrorNet.Visibility = System.Windows.Visibility.Collapsed;
                gContentResult.Visibility = System.Windows.Visibility.Visible;
            }
            catch
            {
                imgLoading.Opacity = 0;
                imgLoading.Stop();
                imgLoading.Visibility = System.Windows.Visibility.Collapsed; 
                gErrorNet.Visibility = System.Windows.Visibility.Visible;
                gContentResult.Visibility = System.Windows.Visibility.Collapsed;
            }
            
        }

        private void imgPreviousResult_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (_CurrentTab == Tab.HotVideo || _CurrentTab == Tab.NewVideo || _CurrentTab == Tab.Top100)
                return;
            if (_CurrentTab == Tab.Search)
            {
                if (_CurrentTabSong == TabSong.Songs)
                    return;
            }

            if (_CurrentTabSong == TabSong.Albums)
            {
                _CurrentTabSong = TabSong.Videos;
                tblCurrentSongTabs.Text = "Video";
                

            }
            else if (_CurrentTabSong == TabSong.Videos)
            {
                _CurrentTabSong = TabSong.Songs;
                tblCurrentSongTabs.Text = "Bài Hát";
                
            }
            else
            {
                _CurrentTabSong = TabSong.Albums;
                tblCurrentSongTabs.Text = "Album";
                
            }
            SetMainList();
        }

        private void imgNextResult_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (_CurrentTab == Tab.HotVideo || _CurrentTab == Tab.NewVideo || _CurrentTab == Tab.Top100)
                return;
            if (_CurrentTab == Tab.Search)
            {
                if (_CurrentTabSong == TabSong.Videos)
                    return;
            }

            if (_CurrentTabSong == TabSong.Songs)
            {
                _CurrentTabSong = TabSong.Videos;
                tblCurrentSongTabs.Text = "Video";
            }
            else if (_CurrentTabSong == TabSong.Albums)
            {
                _CurrentTabSong = TabSong.Songs;
                tblCurrentSongTabs.Text = "Bài Hát";

            }
            else
            {
                _CurrentTabSong = TabSong.Albums;
                tblCurrentSongTabs.Text = "Album";

            }
            SetMainList();
        }

        #endregion

        #region Grid Main Content Tap

        private void SetShowHiddenGrid(bool showGMainContent, bool showGExecuteContentTop, bool showGExecuteContentTop100
            , bool showGExecuteContentSearch, bool showGResultContent)
        {
            if (showGMainContent)
            {
                gMainContent.Visibility = System.Windows.Visibility.Visible;
                gExecuteContent.Visibility = System.Windows.Visibility.Collapsed;
                gResultContent.Visibility = System.Windows.Visibility.Collapsed;
                return;
            }

            if (showGExecuteContentTop)
            {
                gMainContent.Visibility = System.Windows.Visibility.Collapsed;
                gExecuteContent.Visibility = System.Windows.Visibility.Visible;
                gResultContent.Visibility = System.Windows.Visibility.Collapsed;
                gExecuteContentTop.Visibility = System.Windows.Visibility.Visible;
                gExecuteContentTop100.Visibility = System.Windows.Visibility.Collapsed;
                gExecuteConentSearch.Visibility = System.Windows.Visibility.Collapsed;
                return;
            }

            if (showGExecuteContentTop100)
            {
                gMainContent.Visibility = System.Windows.Visibility.Collapsed;
                gExecuteContent.Visibility = System.Windows.Visibility.Visible;
                gResultContent.Visibility = System.Windows.Visibility.Collapsed;
                gExecuteContentTop.Visibility = System.Windows.Visibility.Collapsed;
                gExecuteContentTop100.Visibility = System.Windows.Visibility.Visible;
                gExecuteConentSearch.Visibility = System.Windows.Visibility.Collapsed;
                return;
            }
            if (showGExecuteContentSearch)
            {
                gMainContent.Visibility = System.Windows.Visibility.Collapsed;
                gExecuteContent.Visibility = System.Windows.Visibility.Visible;
                gResultContent.Visibility = System.Windows.Visibility.Collapsed;
                gExecuteContentTop.Visibility = System.Windows.Visibility.Collapsed;
                gExecuteContentTop100.Visibility = System.Windows.Visibility.Collapsed;
                gExecuteConentSearch.Visibility = System.Windows.Visibility.Visible;
                return;
            }
            if (showGResultContent)
            {
                gMainContent.Visibility = System.Windows.Visibility.Collapsed;
                gExecuteContent.Visibility = System.Windows.Visibility.Collapsed;
                gResultContent.Visibility = System.Windows.Visibility.Visible;
                gContentResult.Visibility = System.Windows.Visibility.Visible;
                gSongTab.Visibility = System.Windows.Visibility.Visible;
                return;
            }
        }

        private void gMainContentSearch_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            _CurrentTab = Tab.Search;
            SetShowHiddenGrid(false, false, false, true, false);
            _PageContent.SetValue(false, false, false, true, false);
        }

        private void gMainContentHome_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTab = Tab.Home;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
                SetMainList();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gMainContentTop_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            _CurrentTab = Tab.Top;
            SetShowHiddenGrid(false, true, false, false, false);
            _PageContent.SetValue(false, true, false, false, false);
        }

        private void gMainContentTop100_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTab = Tab.Top100;
                SetShowHiddenGrid(false, false, true, false, false);
                _PageContent.SetValue(false, false, true, false, false);
                _CurrentTabSong = TabSong.Songs;    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gMainContentHotVideo_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTab = Tab.HotVideo;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Videos;
                SetMainList();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gMainContentHotVideoNew_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTab = Tab.NewVideo;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Videos;
                SetMainList();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region Grid Search Content
        private async void btSearch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string key = txtKeySearch.Text;
                if (key == "")
                    return;
                await _Mp3Zing.SetSearch(key);
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
               // await SetSongList();
                SetMainList();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        #endregion

        #region Grid Top Content Tap
        private async void gExecuteContentTopVN_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTopType = TopType.VN;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
                //await SetSongList();
                SetMainList();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private async void gExecuteContentTopUS_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTopType = TopType.US;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
                //await SetSongList();
                SetMainList();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void gExecuteContentTopKR_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTopType = TopType.KR;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
                //await SetSongList();
                SetMainList();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region Grid Top 100 Tap

        private async void gTop1OONhacThieuNhi_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTop100Type = Top100Type.NhacThieuNhi;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
               // await SetSongList();
                SetMainList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void gTop1OONhacTruTinh_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
             try
            {
                _CurrentTop100Type = Top100Type.NhacTruTinh;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
                //await SetSongList();
                SetMainList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void gTop1OONhacRapViet_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTop100Type = Top100Type.NhacRapViet;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
                //await SetSongList();
                SetMainList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void gTop1OONhacRockViet_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTop100Type = Top100Type.NhacRockViet;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
                //await SetSongList();
                SetMainList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void gTop1OONhacCachMang_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTop100Type = Top100Type.NhacCachMang;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
                //await SetSongList();
                SetMainList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void gTop1OONhacTrinh_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTop100Type = Top100Type.NhacTrinh;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
                //await SetSongList();
                SetMainList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void gTop1OONhacTre_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTop100Type = Top100Type.NhacTre;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
                //await SetSongList();
                SetMainList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void gTop1OOPop_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTop100Type = Top100Type.Pop;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
               // await SetSongList();
                SetMainList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void gTop1OORock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTop100Type = Top100Type.Rock;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
                //await SetSongList();
                SetMainList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void gTop1OORap_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTop100Type = Top100Type.Rap;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
                //await SetSongList();
                SetMainList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private async void gTop1OOCountry_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                _CurrentTop100Type = Top100Type.Country;
                SetShowHiddenGrid(false, false, false, false, true);
                _PageContent.SetValue(false, false, false, false, true);
                _CurrentTabSong = TabSong.Songs;
                //await SetSongList();
                SetMainList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

       #endregion

        #region Main List Item
        private async void lMainList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var myIndex = ((LongListSelector)sender).ItemsSource.IndexOf(lMainList.SelectedItem);
                //string link = "";
                if (_CurrentTabSong == TabSong.Songs)
                {
                    try
                    {
                        if (_ListSong[myIndex].DirectLink == "")
                            await _ListSong[myIndex].SetAllInformation();
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message == "NetError1908")
                        {
                            MessageBox.Show("Lỗi kết nối. Vui lòng kiểm tra lại kết nối internet.");
                        }
                        else
                        {
                            MessageBox.Show("Nội dung yêu cầu hiện không tồn tại. Vui lòng kiểm tra lại.");
                        }
                        return;
                    }
                    PhoneApplicationService.Current.State["param"] = _ListSong[myIndex];
                    NavigationService.Navigate(new Uri("/PlayMusic.xaml", UriKind.Relative));

                }
                else if (_CurrentTabSong == TabSong.Videos)
                {
                    try
                    {
                        if (_ListVideo[myIndex].DirectLink == "")
                            await _ListVideo[myIndex].SetAllInformation();
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message == "NetError1908")
                        {
                            MessageBox.Show("Lỗi kết nối. Vui lòng kiểm tra lại kết nối internet.");
                        }
                        else
                        {
                            MessageBox.Show("Nội dung này tạm thời bị ẩn theo yêu cầu của đơn vị sở hữu bản quyền.");
                        }
                        return;
                    }
                    PhoneApplicationService.Current.State["param"] = _ListVideo[myIndex];
                    NavigationService.Navigate(new Uri("/PlayVideo.xaml", UriKind.Relative));

                }
                else
                {
                    try
                    {
                        if (_ListAlbum[myIndex].ListSong.Count <= 0)
                            await _ListAlbum[myIndex].SetAllInformation();
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message == "NetError1908")
                        {
                            MessageBox.Show("Lỗi kết nối. Vui lòng kiểm tra lại kết nối internet.");
                        }
                        else
                        {
                            MessageBox.Show("Nội dung yêu cầu hiện không tồn tại. Vui lòng kiểm tra lại.");
                        }
                        return;
                    }

                    PhoneApplicationService.Current.State["param"] = _ListAlbum[myIndex];
                    NavigationService.Navigate(new Uri("/PlayVideo.xaml", UriKind.Relative));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region OTHER EVNETS

        private void Image_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            SetShowHiddenGrid(true, false, false, false, false);
            _PageContent.SetValue(true, false, false, false, false);
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            base.OnBackKeyPress(e);
            MessageBoxResult result = MessageBox.Show("Bạn có muốn thoát?", "THÔNG BÁO", MessageBoxButton.OKCancel);
            if (result == MessageBoxResult.OK)
            {
                while (this.NavigationService.BackStack.Any())
                {
                    this.NavigationService.RemoveBackEntry();
                }
                e.Cancel = false;
            }
            else
                e.Cancel = true;
        }

        private void Status_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _MouseDownPoint = e.GetPosition(gStatus);
        }

        private void Status_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _MouseUpPoint = e.GetPosition(gStatus);
            if (_MouseDownPoint.X > _MouseUpPoint.X && _MouseUpPoint.Y >= _MouseDownPoint.Y - 10 && _MouseUpPoint.Y <= _MouseDownPoint.Y + 10)
            {
                string uri = "/PhonePage.xaml";
                NavigationService.Navigate(new Uri(uri, UriKind.Relative));
            }
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            _PageContent.Mp3Zing = _Mp3Zing;
            _PageContent.CurrentTab = _CurrentTab;
            _PageContent.CurrentTabSong = _CurrentTabSong;
            _PageContent.CurrentTopType = _CurrentTopType;
            _PageContent.CurrentTop100Type = _CurrentTop100Type;
            PhoneApplicationService.Current.State["Mp3ZingPage"] = _PageContent;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (PhoneApplicationService.Current.State.ContainsKey("Mp3ZingPage"))
            {
                Mp3ZingPageControl pagecontrol = null;
                try
                {
                    pagecontrol = PhoneApplicationService.Current.State["Mp3ZingPage"] as Mp3ZingPageControl;
                }
                catch
                { }
                if (pagecontrol != null)
                    _PageContent = pagecontrol;
            }
        }

        #endregion  
    }
}